package com.asset.modules.common.servcie;

import com.asset.modules.assets.model.Asset;
import com.asset.modules.assets.service.AssetService;
import com.asset.modules.common.utils.Constant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenchen
 */
@Configuration
@EnableScheduling
public class ScheduleService {

    @Autowired
    AssetService assetService;

    /**
     * 自动执行固资状态重置与净值计算任务
     * 每年最后一天凌晨执行
     */
    @Scheduled(cron = "0 0 0 01 03 ?")
    private void updateAssetValue(){
        //获取所有盘点过的固资
        QueryWrapper<Asset> qw  = new QueryWrapper<>();
        qw.eq("status", Constant.checkedStatus);
        List<Asset> assets  = assetService.list(qw);
        List<Asset> newList = new ArrayList<>();
        if(assets!=null&&assets.size()>0){

            for(Asset a : assets){
                //重置盘点状态
                a.setStatus(Constant.unCheckedStatus);
                //设置净值
                a.setValue(CalcValueService.calcAssetValue(a.getRegisterMoney(), a.getTotalYear(), a.getBuyTime()));
                newList.add(a);
            }
            //批量保存数据
            assetService.saveOrUpdateBatch(newList);
        }

    }


}
